﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Dashboard.Expense.Business;

namespace Dashboard.Expense
{
    public partial class FormAddExpense : Form
    {
        public FormAddExpense()
        {
            InitializeComponent();
        }

        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            if (this.comboBoxTypes.SelectedItem == null)
            {
                MessageBox.Show(this, "Please select the Application Type.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.comboBoxTypes.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.textBoxCode.Text.Trim()))
            {
                MessageBox.Show(this, "Please input the Type Code field.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.textBoxCode.Focus();
                return;
            }

            try
            {
                int typeID = int.Parse((this.comboBoxTypes.SelectedItem as ComboBoxItem).Value.ToString());
                string code = this.textBoxCode.Text.Trim();
                string description = this.textBoxDescription.Text.Trim();
                string expenseType = this.comboBoxExpenseType.Text;

                Exp_ExpenseType eet = new Exp_ExpenseType();
                eet.AddExpense(typeID, code, description, expenseType);

                MessageBox.Show(this, "Save successfully!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.textBoxCode.Text = string.Empty;
                this.textBoxDescription.Text = string.Empty;
                this.comboBoxExpenseType.Text = string.Empty;
                this.textBoxCode.Focus();
                //this.DialogResult = System.Windows.Forms.DialogResult.OK;
                //this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void toolStripButtonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void FormAddExpense_Load(object sender, EventArgs e)
        {
            this.BindTypes();

            this.BindExpenseTypes();
        }

        private void BindTypes()
        {
            try
            {
                DataTable types = new Exp_ExpenseType().QueryApplicationTypes();
                this.comboBoxTypes.Items.Clear();
                foreach (DataRow row in types.Rows)
                {
                    this.comboBoxTypes.Items.Add(new ComboBoxItem() { Text = row["Code"].ToString(), Value = int.Parse(row["id"].ToString()) });
                }

                this.comboBoxTypes.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }
        }

        private void BindExpenseTypes()
        {
            this.comboBoxExpenseType.Items.Clear();
            this.comboBoxExpenseType.Items.Add(ExpenseTypes.Normal);
            this.comboBoxExpenseType.Items.Add(ExpenseTypes.Breakfast);
            this.comboBoxExpenseType.Items.Add(ExpenseTypes.Lunch);
            this.comboBoxExpenseType.Items.Add(ExpenseTypes.Supper);
            this.comboBoxExpenseType.Items.Add(ExpenseTypes.Hotel);

            this.comboBoxExpenseType.SelectedIndex = 0;
        }
    }

    public class ComboBoxItem
    {
        public string Text { get; set; }
        public object Value { get; set; }
        public override string ToString()
        {
            return this.Text;
        }
    }
}
